package client_controllers;

import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;





import client_controllers.CustomerServiceController.Exit;
import client_controllers.CustomerServiceController.Logout;
import client_controllers.CustomerServiceController.SubmitListener;
import client_controllers.MarketingEmployeeController.AnalysisSystemListener;
import client_gui.CustomerServiceGUI;
import client_gui.ProductGUI;
import client_gui.ProductGUI;
import client_logic.CcrmClient;
import client_logic.dbHandler;
import entity.Boss;
import entity.CampaignPattern;
import entity.Customer;
import entity.Product;

public class ProductController implements dbHandler,Serializable{
	/**
	 * @class ProductController allows communications between marketing employees and DB to extract customer information
	 * 
	 * @param ProductGUI links to GUI.
	 * @param EmployeeLoginController links to check fields for employee authorization. 
	 * @param Boss is used as an instance, to send objects to DB and allow changes in the lists.
	 * @param MarketingEmployeeController links to check fields for employee authorization.
	 * @param ProductController links to product lists.
	 * @param Product is for a new product instance.
	 */
	
	private ProductGUI pg;
	private EmployeeLoginController elc;
	private MarketingEmployeeController mec;
	private ProductController pc;
	private Product p;
	private Boss b;
	
	public ProductController(ProductGUI pg,EmployeeLoginController elc,MarketingEmployeeController mec){ 
		pc=this;
		this.setPg(pg);
		this.setElc(elc);
		this.setMec(mec);
		this.pg.getBtnAddProduct().addActionListener(new AddItemListener());
		this.pg.getBtnRemoveProduct().addActionListener(new RemoveItemListener());
		this.pg.getBtnBack().addActionListener(new BackListener());
		//get product names to catalog 
		getProductNames();
	}

	private void getProductNames() {

		p= new Product();
		this.setBoss(new Boss(p,"getProductNames","ProductController"));
		this.Search();
		
	}
	public class BackListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			pg.dispose();
			mec.getMeg().setVisible(true);
			
			
		}
	}

	public class AddItemListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			// add item to catalog
			p = new Product ();
			
			BufferedImage img =null;
			try{
			p.setName(pg.getName());
			String LocalfilePath=pg.getPhoto().getText();
			p.setPrice(pg.getPrice());
			p.setDescription(pg.getDescription());
			
			try{
				File newFile = new File (LocalfilePath);
				 byte [] mybytearray  = new byte [(int)newFile.length()];
				
				FileInputStream fis = new FileInputStream(newFile);
				 BufferedInputStream bis = new BufferedInputStream(fis);			  
				
				 
				  
				  p.initArray(mybytearray.length);
				  p.setSize(mybytearray.length);
				 bis.read(p.getMybytearray(),0,mybytearray.length);
				 bis.read();

				pc.setBoss(new Boss(p,"addProduct","ProductController"));
				pc.Add();
			}catch(Exception e) {
				System.out.println("Error send (Files)msg) to Server");
			}
			}catch(Exception ex){
				pg.setLblMsg("fields missing "); 
				pg.getLblMsg().setVisible(true);
				
			}
			
		}
	}

	public class RemoveItemListener implements ActionListener{
		public void actionPerformed (ActionEvent ev){
			// get product names into
			p = new Product();
			p.setName((String)pg.getComboBox().getSelectedItem());

			pc.setBoss(new Boss(p,"deleteProduct","ProductController"));
			pc.Delete();
			
		}
	}

	public MarketingEmployeeController getMec(){
		return this.mec;
	}
	private void setMec(MarketingEmployeeController mec) {
		this.mec=mec;
	}
	public ProductGUI getPcg(){
		return this.pg;
	}
	public void setPg(ProductGUI pg){
		this.pg=pg;
	}
	public Product getProduct(){
		return this.p;
	}
	public void setProduct (Product p){
		this.p=p;
	}
	public Boss getBoss(){
		return this.b;
	}
	public void setBoss(Boss b){
		this.b=b;
	}
	public void setElc(EmployeeLoginController elc) {
		this.elc = elc;
	}

	@Override
	public void Add() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public void Search() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
	}

	@Override
	public String Delete() {
		try{CcrmClient.inet.setCurrCon(this);
		CcrmClient.inet.sendToServer(b);
			
		}catch (IOException ex){
			ex.printStackTrace();
		}
		
		return null;
	}

	@Override
	public void HandleMsg(Object msg) {
		this.setBoss((Boss) msg);
		if(this.getBoss().getCommand().equals("getProductNames")){
		ArrayList <String> names ;
		names= (ArrayList<String>) this.getBoss().getObj();
		for(String i :names)
			pg.getComboBox().addItem(i); //change the attribute 

		}
		if(this.getBoss().getCommand().equals("deleteProduct")){
				if(this.getBoss().getMsg().equals("product deleted successefully")){
					
					pg.setLblMsg("product deleted successefully");
					pg.getLblMsg().setForeground(Color.green);
					pg.getLblMsg().setVisible(true);
					pg.getComboBox().removeAllItems();
					this.getProductNames();
				}
				else {
					pg.setLblMsg("could not delete this product");
					pg.getLblMsg().setForeground(Color.red);
					pg.getLblMsg().setVisible(true);
				}
						

		}
		
		if(this.getBoss().getCommand().equals("addProduct")){
			if(this.getBoss().getMsg().equals("product added successefully")){
				
				pg.setLblMsg("product added successefully");
				pg.getLblMsg().setForeground(Color.green);
				pg.getLblMsg().setVisible(true);
				pg.getComboBox().removeAllItems();
				this.getProductNames();
			}
			else {
				pg.setLblMsg("could not add this product");
				pg.getLblMsg().setForeground(Color.red);
				pg.getLblMsg().setVisible(true);
			}
					

	}
		 
		
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}	
}
